package org.lanqiao.tjut.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.lanqiao.tjut.bean.UsersBean;
import org.lanqiao.tjut.db.DBDriverFactory;

import sun.text.normalizer.UBiDiProps;

public class UserInfoManagerDao {

	/**
	 * 保存用户信息
	 * 
	 * @param uBean
	 *            用户信息对象
	 * @return 保存结果记录行数
	 */
	public int savaUserInfo(UsersBean uBean) {
		int re_i = 0;
		if (uBean.getUserid() == null) {
			// 新增处理
			String strSql = "INSERT INTO PDUUSERS(USERNAME,USERPSW,USERSEX,USERAGE,USERTEL,USERADDRESS)VALUES(?,?,?,?,?,?)";
			re_i = DBDriverFactory.getDBDriverInstance().update(strSql,
					new Object[] { uBean.getUsername(), uBean.getUserpsw(), uBean.getUsersex(), uBean.getUserage(),
							uBean.getUsertel(), uBean.getUseraddress() });
		} else {
			// 修改处理
			String strSql = "UPDATE PDUUSERS  SET USERNAME =?,USERPSW=?,USERSEX=?,USERAGE=?,USERTEL=?,USERADDRESS=? WHERE USERID =? ";
			re_i = DBDriverFactory.getDBDriverInstance()
					.update(strSql,
							new Object[] { uBean.getUsername(), uBean.getUserpsw(), uBean.getUsersex(),
									uBean.getUserage(), uBean.getUsertel(), uBean.getUseraddress(),
									uBean.getUserid() });

		}
		return re_i;
	}

	/**
	 * 查询所有用户数据从信息
	 * 
	 * @return 所有用户数据信息集合
	 */
	public List<UsersBean> getAllUserInfo() {
		String strSql = "SELECT * FROM PDUUSERS ORDER BY USERID DESC";
		List<UsersBean> lstuser = DBDriverFactory.getDBDriverInstance().query(strSql,
				new BeanListHandler<UsersBean>(UsersBean.class));
		return lstuser;
	}

	/**
	 * 查询用户数据信息(模糊查询)
	 * 
	 * @param uBean
	 *            用户信息对象
	 * @return 查询的用户信息
	 */
	public List<UsersBean> getQueryUserInfo(UsersBean uBean) {
		// 根据条件进行数据查询，没有输入查询条件，则进行全部查询

		StringBuilder strSql = new StringBuilder("SELECT * FROM PDUUSERS WHERE 1=1");

		// 根据查询条件动态拼写查询语句和参数
		List<Object> lstParams = new ArrayList<Object>();

		if (uBean.getUsername() != null && !uBean.getUsername().trim().equals("")) {
			// 姓名查询

			strSql.append(" AND USERNAME LIKE ?");
			lstParams.add("%%" + uBean.getUsername() + "%%");
		}
		if (uBean.getUsersex() != null && !uBean.getUsersex().trim().equals("")) {
			// 性别查询
			strSql.append(" AND USERSEX LIKE ?");
			lstParams.add("%%" + uBean.getUsersex() + "%%");
		}
		if (uBean.getUserage() > 0) {
			// 年龄查询
			strSql.append(" AND USERAGE >= ?");
			lstParams.add(uBean.getUserage());
		}
		if (uBean.getUserage2() > 0) {
			// 年龄查询
			strSql.append(" AND USERAGE <= ?");
			lstParams.add(uBean.getUserage2());
		}
		if (uBean.getUsertel() != null && !uBean.getUsertel().trim().equals("")) {
			// 电话查询
			strSql.append(" AND USERTEL LIKE ?");

			lstParams.add("%%" + uBean.getUsertel() + "%%");
		}
		if (uBean.getUseraddress() != null && !uBean.getUseraddress().trim().equals("")) {
			// 电话查询
			strSql.append(" AND USERADDRESS LIKE ?");

			lstParams.add("%%" + uBean.getUseraddress() + "%%");
		}
		// 给查询语句添加排序
		strSql.append(" ORDER BY USERID");

		List<UsersBean> lstuser = DBDriverFactory.getDBDriverInstance().query(strSql.toString(),
				new BeanListHandler<UsersBean>(UsersBean.class), lstParams.toArray());

		return lstuser;
	}

	/**
	 * 根据用户id删除该用户信息
	 * 
	 * @param userId
	 *            用户id
	 * @return 修改记录的行数
	 */
	public int deleteUserInfo(int userId) {
		String strSql = "DELETE  FROM PDUUSERS WHERE USERID = ?";
		int re_i = DBDriverFactory.getDBDriverInstance().update(strSql, new Object[] { userId });
		return re_i;
	}

	/**
	 * 通过用户id获取主键对象
	 * 
	 * @param strUid
	 *            用户id
	 * @return 对象
	 */
	public UsersBean getUserInfoById(String strUid) {
		String strSql = "SELECT * FROM PDUUSERS WHERE USERID =?";
		UsersBean Ubean = DBDriverFactory.getDBDriverInstance().query(strSql,
				new BeanHandler<UsersBean>(UsersBean.class), new Object[] { strUid });
		return Ubean;
	}
}
